package com.copa.gamestore.security.domain;

import com.copa.gamestore.module.user.application.UserApplicationService;
import com.copa.gamestore.module.user.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-09-21 21:55
 * @function 认证用户模型
 * 用户注册之后，包含其业务属性，如姓名、电话、地址，用于业务发生，存储于User对象中
 * 也包含其用于认证的属性，譬如密码、角色、是否停用，存储于AuthenticUser对象中
 */
public class AuthenticUser extends User implements UserDetails {

    /**
     * 该用户拥有的授权，譬如读取权限、修改权限、增加权限等等
     */
    private Collection<GrantedAuthority> authorities = new HashSet<>();

    public AuthenticUser(User user, List<String> roleNameList) {
        BeanUtils.copyProperties(user, this);
        roleNameList.forEach(name -> authorities.add(new SimpleGrantedAuthority(name)));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public String getUsername() {
        return super.getUsername();
    }

    /**
     * 账号是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return !getIsExpire();
    }

    /**
     * 是否锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return !getIsLock();
    }

    /**
     * 密码是否过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return !getPasswordExpire();
    }

    /**
     * 是否被锁定
     */
    @Override
    public boolean isEnabled() {
        return !getIsEnable();
    }
}
